<template>
  <div>
    <el-container>
      <el-header>
        <div class="huanyingci">欢迎光临，人人旅游网！</div>
        <div style="float: right">
          <el-link
            type="primary"
            class="denglu"
            @click="goDenglu"
            v-show="denglu"
            >登录</el-link
          >
          <el-link
            type="primary"
            class="tuichu"
            @click="personalIndex"
            v-show="doTuichu"
            >个人中心</el-link
          >
          <el-link
            type="primary"
            class="tuichu"
            @click="doLogout"
            v-show="doTuichu"
            >退出</el-link
          >
        </div>
      </el-header>
      <el-main>
        <el-card class="box-card">
          <div v-for="o in content" :key="o.id" class="text item">
            <div style="overflow: hidden">
              <h2>{{ o.nickname }}</h2>
              <img
                :src="o.picture"
                style="width: 200px; height: 200px; float: left"
              />
              <h2>{{ o.title }}</h2>
              <h3>{{ o.ccity }}</h3>
              <h4>{{ o.introduction }}</h4>
              <p>{{ o.des }}</p>
            </div>

            <div class="liaotian">
                <el-button size="small" @click="getGategory(o.id)"
                  >评论</el-button
                >
            </div>
          </div>
        </el-card>
      </el-main>
    </el-container>
    <!-- 聊天弹框start -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      size="50%"
      ref="drawer"
      :modal="false"
    >
      <el-card class="box-card">
        <div v-for="item in gategory" :key="item.id" class="text item">
          <h2>{{ item.nickname }}</h2>
          {{ item.content }}
        </div>
      </el-card>
      <div class="fabukuan" v-show="fabu">
        <el-input
          v-model="con"
          placeholder="请输入内容"
          style="width: 93%"
        ></el-input>
        <el-button
          style="margin-left: 20px"
          type="primary"
          @click="fabugategory"
          >发布</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>
<script>
export default {
  name: "userIndex",
  data() {
    return {
      content: [],
      drawer: false,
      direction: "btt",
      gategory: [],
      con: "",
      fabu: false,
      cid: "",
      denglu: true,
      doTuichu: true,
    };
  },
  created() {
    this.getCountAll();
  },
  computed:{
    nickname(){
      return JSON.parse(localStorage.getItem("login_user")).nickname;
    }
  },
  mounted() {
    let login_user = JSON.parse(localStorage.getItem("login_user"));
    if (login_user) {
      this.denglu = false;
    }
    if (!login_user) {
      this.doTuichu = false;
    }
  },
  methods: {
    getCountAll() {
      let url = "/contentController/getContent";
      let param = "";
      this.$axios
        .get(url, param)
        .then((resp) => {
          this.content = resp.data;
        })
        .catch((err) => {
          console.log("error=", err);
        });
    },
    getGategory(id) {
      this.drawer = true;
      this.fabu = true;
      this.cid = id;
      let content = {
        cid: id,
      };
      let url = "/GuestbookController/getGuestByCid";
      let param = this.$qs.stringify(content);
      this.$axios
        .post(url, param)
        .then((resp) => {
          this.gategory = resp.data;
        })
        .catch((err) => {
          console.log("error=", err);
        });
    },
    fabugategory() {
      let login_user = JSON.parse(localStorage.getItem("login_user"));
      console.log(login_user);
      if (!login_user) {
        this.$message({
          type: "error",
          message: "请先登录!",
        });
        return;
      }
      let guestbook = {
        content: this.con,
        cid: this.cid,
        uid: login_user.id,
      };
      let url = "/GuestbookController/addGuestbook";
      let param = this.$qs.stringify(guestbook);
      this.$axios
        .post(url, param)
        .then((resp) => {
          if (resp.data) {
            this.getGategory(this.cid);
            this.con = "";
          }
        })
        .catch((err) => {
          console.log("error=", err);
        });
    },
    goDenglu() {
      this.$router.push("/Login");
    },
    doLogout() {
      // let url='/LoginController';
      let url = "/loginController/logout";
      let param = "act=logout";
      this.$axios
        .post(url, param)
        .then((resp) => {
          localStorage.removeItem("login_user");
          this.$router.push("/Login");
        })
        .catch((err) => {
          console.log("err=", err);
        });
    },
    personalIndex() {
      this.$router.push("/personalIndex");
    },
  },
};
</script>
<style scoped>
.el-header {
  background-color: rgba(245, 241, 235, 0);
  color: #fff;
  text-align: center;
  line-height: 100px;
  font-size: 50px;
  height: 100px !important;
}
.huanyingci {
  float: left;
  font-size: 30px;
}
.denglu {
  position: relative;
  right: 0;
  font-size: 30px;
}
.tuichu {
  position: relative;
  right: 0;
  margin-left: 10px;
  font-size: 30px;
}
.text {
  font-size: 14px;
}
.item {
  padding: 18px 0;
}

.box-card {
  width: 100%;
  background-color: rgba(231, 237, 238, 0.4);
}
.el-main {
  padding: 0 !important;
}
.liaotian {
  width: 100%;
  height: 50px;
  position: relative;
  top: 0px;
  left: 90%;
}
/deep/.el-drawer__header {
  padding: 0 !important;
  margin: 0 !important;
}
.fabukuan {
  position: fixed;
  bottom: 0px;
  width: 100%;
}
</style>